﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class Reports_BI_KPI_MainPanel : System.Web.UI.Page
{

    #region Members

    StringBuilder sbTbl = new StringBuilder();
    string nl = @"
";
    DataTable tblData;
    int TblCurRow = 0;
    DateTime dt = DateTime.Now;
    int rowCount = 0;
    int rowAll = 0;
    int MonthStart = 1;
    int MonthStop = 1;
    int TurnoverFull = 0;
    int PlanFull = 0;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ret = null;

        oCaption.Text = "Сегодня - " + String.Format("{0:00} / ", dt.Month) + dt.Year;

        ret = MakeData();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        ret = MakeHTML_Table();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        MakeHTML_Table();
    }

    private void MakeDataTable()
    {
        tblData = new DataTable("Chart");
        tblData.Columns.Add("Секция", typeof(string));
        tblData.Columns.Add("Оборот", typeof(int));
        tblData.Columns.Add("План", typeof(int));
        tblData.Columns.Add("Прогноз", typeof(int));
        tblData.Columns.Add("Отклонение", typeof(Decimal));
    }

    private Exception MakeData()
    {
        Exception ret = null;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);
        MonthStart = dt.Month;
        MonthStop = dt.Month;
        MakeDataTable();
        ret = FillDataTable();
        if (ret != null) return ret;
        return ret;
    }

    Exception FillDataTable()
    {

        Exception ret = null;
        rowAll = 0;
        rowCount = 0;
        for (int i = MonthStart; i <= MonthStop; i++)
        {
            TurnoverFull = 0;
            PlanFull = 0;
            DataTable tblSrc = new DataTable("tbl");
            Exception err = new Ko2.DB.BI.KPI.MainPanel().Get(ref tblSrc, dt.Year, i);
            if (err != null)
            {
                ret = new Exception(err.Message, err);
                return ret;
            }
            foreach (DataRow r in tblSrc.Rows)
            {
                if (r["Name"] == DBNull.Value
                    //|| r["Name"].ToString() == "Все"
                    )
                {
                    continue;
                }
                DataRow newRow = tblData.NewRow();
                newRow["Секция"] = "";
                if (r["Name"] != DBNull.Value)
                {
                    newRow["Секция"] = r["Name"];
                }
                newRow["Оборот"] = 0;
                if (r["Turnover"] != DBNull.Value)
                {
                    newRow["Оборот"] = Convert.ToInt32(r["Turnover"]);
                    TurnoverFull += Convert.ToInt32(r["Turnover"]);
                }
                newRow["План"] = 0;
                if (r["Plan"] != DBNull.Value)
                {
                    newRow["План"] = Convert.ToInt32(r["Plan"]);
                    PlanFull += Convert.ToInt32(r["Plan"]);
                }
                newRow["Прогноз"] = 0;
                if (r["Forecast"] != DBNull.Value)
                {
                    newRow["Прогноз"] = Convert.ToInt32(r["Forecast"]);
                }
                newRow["Отклонение"] = 0;
                if (r["Deviation"] != DBNull.Value)
                {
                    newRow["Отклонение"] = Convert.ToDecimal(r["Deviation"]);
                }

                tblData.Rows.Add(newRow);
                rowAll++;
            }
        }

        return ret;
    }

    Exception MakeHTML_Table()
    {
        Exception ret = null;
        TblCurRow = -1;
        sbTbl.Append(
@"
<script language='javascript'>
//<![CDATA[
    function drawTable() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Секция');
        data.addColumn('number', 'Оборот, Руб');
        data.addColumn('number', 'План на месяц');
        data.addColumn('number', 'Прогноз, руб');
        data.addColumn('number', 'Отклонение');
        data.addRows(" + tblData.Rows.Count + @"); 
       
");

        foreach (DataRow r in tblData.Rows)
            TblAddRow((string)r["Секция"], (int)r["Оборот"], (int)r["План"], (int)r["Прогноз"], (Decimal)r["Отклонение"]);

        sbTbl.Append(@"
        var table = new google.visualization.Table(document.getElementById('table_div'));
        table.draw(data, {showRowNumber: false, sortColumn: 1, sortAscending: false});

        var data2 = new google.visualization.DataTable();
        data2.addColumn('string', 'Task');
        data2.addColumn('number', 'Hours per Day');
        data2.addRows(" + (tblData.Rows.Count -1) + @");");

        TblCurRow = -1;
        foreach (DataRow r in tblData.Rows)
        {
            if ((string)r["Секция"] == "Все")
                continue;
            TblCurRow++;
            sbTbl.Append(@"
            data2.setValue(" + TblCurRow + @", 0, '" + r["Секция"] + @"');
            data2.setValue(" + TblCurRow + @", 1, " + (int)r["Оборот"] + @");
            ");

        }

        sbTbl.Append(@"
        var oChartDiv2 = document.getElementById('table_div2');
        new google.visualization.PieChart(oChartDiv2).draw(data2, {title:""Оборот""});
");


        sbTbl.Append(@"
        var data3 = new google.visualization.DataTable();
        data3.addColumn('string', 'Task');
        data3.addColumn('number', 'Hours per Day');
        data3.addRows(" + (tblData.Rows.Count - 1) + @");");

        TblCurRow = -1;
        foreach (DataRow r in tblData.Rows)
        {
            if ((string)r["Секция"] == "Все")
                continue;
            TblCurRow++;
            sbTbl.Append(@"
            data3.setValue(" + TblCurRow + @", 0, '" + r["Секция"] + @"');
            data3.setValue(" + TblCurRow + @", 1, " + (int)r["План"] + @");
            ");

        }

        sbTbl.Append(@"
        var oChartDiv3 = document.getElementById('table_div3');
        new google.visualization.PieChart(oChartDiv3).draw(data3, {title:""План""});
");

        sbTbl.Append(@"
    }
//]]>
</script>");


        oBody.Text = sbTbl.ToString();
        return ret;
    }

    Exception TblAddRow(string name, int val1, int val2, int val3, Decimal val4)
    {
        Exception ret = null;
        TblCurRow++;
        sbTbl.Append(@"
data.setCell(" + TblCurRow + @", 0, '" + name + @"');");

        string str = val1.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 1, " + val1 + @", '" + str + @"');
");

        str = val2.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 2, " + val2 + @", '" + str + @"');
");

        str = val3.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 3, " + val3 + @", '" + str + @"');
");

        str = String.Format("{0:0.00}", (val4 * (Decimal)100.0) ).Replace(",", ".");
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 4, " + str + @", '" + str + " % " + @"');
");

        return ret;
    }

}